home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / game / role / ldmud-3.2-bin.lha / mud / mudlib / simul_efun-dr.c < prev    next >
C/C++ Source or Header  |  1999-03-31  |  11KB  |  450 lines

  1. /* This is an addition to the simul_efun file for a fast migration from
  2.  * the -DR parser. Only the ls() function of the original simul_efun need to
  3.  * be commented out
  4.  */
  5. #include "/sys/lpctypes.h"
  6. #define ATTRIBUTE_INDEX 2
  7. #define TEXT_WIDTH 70
  8.  
  9. /*
  10.  * check_ref() is only a fake function.
  11.  * missing: query_arg/this_arg(), remove_action()
  12.  *
  13.  * The following defines might be appropriate when you need to stay with
  14.  * old code and want speed. Put them in an appropriate header file or in
  15.  * the auto_include string.
  16.  *
  17.  * #define caller() previous_object()
  18.  * if using OLD_PREVIOUS_OBJECT_BEHAVIOUR :
  19.  * #define caller() previous_object(0) // always works, but slower
  20.  *
  21.  * #define contents(ob) all_inventory((ob))
  22.  * #define deletem(m, d) efun::m_delete((m), (d))
  23.  * #define this_verb() query_verb()
  24.  * #define atoi(str) to_int(str)
  25.  * #define keys(m) m_indices(m)
  26.  * #define mapp(m) mappingp(m)
  27.  *
  28.  * The following defines have a problem: a parameter is used twice.
  29.  * #define deletea(a, start, end) ((a)[0..(start)-1] + (a)[(end)+1..])
  30.  * #define index(a, d, start)   member((start) ? (a)[(start)..] : (a) , (d))
  31.  * #define searcha(a, d, start) member((start) ? (a)[(start)..] : (a) , (d))
  32.  */
  33.  
  34. static void start_simul_efun_dr() {
  35.     mixed *info;
  36.  
  37.     info = get_extra_wizinfo(0);
  38.     if (!info[ATTRIBUTE_INDEX])
  39.     info[ATTRIBUTE_INDEX] = allocate_mapping(0, 1);
  40.     if (find_call_out("clean_simul_efun_dr") < 0)
  41.     call_out("clean_simul_efun_dr", 900);
  42. }
  43.  
  44. static void clean_simul_efun_dr() {
  45.     /* There might be destructed objects as keys. */
  46.     m_indices(get_extra_wizinfo(0)[ATTRIBUTE_INDEX]);
  47.     remove_call_out("clean_simul_efun_dr");
  48.     call_out("clean_simul_efun_dr", 3600);
  49. }
  50.  
  51. varargs void add_attribute(string name, mixed value, object ob) {
  52.     mapping attributes;
  53.  
  54.     if (!ob) {
  55.     if (objectp(value)) {
  56.         ob = value;
  57.         value = 1;
  58.     } else {
  59.         ob = previous_object();
  60.     }
  61.     }
  62.     attributes =
  63.       (attributes = get_extra_wizinfo(0)[ATTRIBUTE_INDEX])[ob] ||
  64.       (attributes[ob] = allocate_mapping(1, 1));
  65.     if (member(attributes, quote(name))) {
  66.     attributes[quote(name)] = value;
  67.     return;
  68.     }
  69.     attributes[name] = value;
  70. }
  71.  
  72. varargs void add_prot_attribute(string name, mixed value, object ob) {
  73.     mapping attributes;
  74.  
  75.     if (!ob) {
  76.     if (objectp(value)) {
  77.         ob = value;
  78.         value = 1;
  79.     } else {
  80.         ob = previous_object();
  81.     }
  82.     }
  83.     efun::m_delete(
  84.       ( (attributes = get_extra_wizinfo(0)[ATTRIBUTE_INDEX])[ob] ||
  85.         (attributes[ob] = allocate_mapping(1, 1)) )
  86.       , name)[quote(name)] = value;
  87. }
  88.  
  89. #ifndef caller
  90. object caller() {
  91.     return previous_object(1);
  92. }
  93. #endif /* caller */
  94.  
  95. /* int */ mixed check_ref() {
  96.     /* There are other ways to debug ref counts. Although a simul_efun is
  97.      * possible, I doubt that it would be worth the trouble
  98.      */
  99.     return 42.42;
  100. }
  101.  
  102. object *contents(object ob) {
  103.     return all_inventory(ob);
  104. }
  105.  
  106. varargs void delete_attribute(string name, object ob) {
  107.     mapping attributes;
  108.  
  109.     if (attributes =
  110.       get_extra_wizinfo(0)[ATTRIBUTE_INDEX][ob || previous_object()])
  111.     {
  112.     if (member(attributes, quote(name)))
  113.         raise_error(
  114.           sprintf("Error, cannot delete protected attribute %s.\n", name));
  115.     if ( !sizeof(efun::m_delete(attributes, name)) )
  116.         efun::m_delete(
  117.           get_extra_wizinfo(0)[ATTRIBUTE_INDEX],
  118.           ob || previous_object()
  119.         );
  120.     }
  121. }
  122.  
  123. varargs mixed deletea(mixed a, int start, int end)
  124. {
  125.     if (!end)            /* This isn't really exact; it should */
  126.       end = start;        /* only be done if end is not given.  */
  127.  
  128.     if (end == sizeof(a) - 1)    /* Avoid "out of bounds" . */
  129.       return a[0..start - 1];
  130.  
  131.     return a[0..start-1] + a[end+1..];
  132. }
  133.  
  134. mapping deletem(mapping m, mixed d) {
  135.     return efun::m_delete(m, d);
  136. }
  137.  
  138. void dumpobjs(int start, int end) {
  139.     object ob;
  140.  
  141.     rm("/OBJDUMP");
  142.     if (end <= 0) {
  143.     printf("Dumping all objects starting with %d.\n", start);
  144.     end = 0x7fffffff;
  145.     } else {
  146.     printf("Dumping objects %d through %d.\n", start, end);
  147.     }
  148.     write_file("/OBJDUMP",
  149.     sprintf("Object                                  Location\n"));
  150.     for (ob = debug_info(start); ob && start <= end;
  151.                     ob = debug_info(2, ob), start++)
  152.        write_file("/OBJDUMP", sprintf("%-40s %s\n", file_name(ob),
  153.                (environment(ob)?file_name(environment(ob)):"None")));
  154.     write("Done.\n");
  155. }
  156.  
  157. int exists(string file) {
  158.     return file_size(file) != -1;
  159. }
  160.  
  161. string *files(string path) {
  162.     mixed *a;
  163.     string *b;
  164.     int i, j;
  165.  
  166.     set_this_object(previous_object());
  167.     a = get_dir(path, 3);
  168.     /* Don't call this locally, unless you add code to set this_object() back */
  169.     b = allocate(j = (i = sizeof(a)) >> 1);
  170.     while (i)
  171.     b[--j] = a[--i] < 0 ? a[--i] + "/" : a[--i];
  172.     return b;
  173. }
  174.  
  175. string format(string text, int width) {
  176.     if (width < 0)
  177.     raise_error("Bad width given to format(); must be >= 0.\n");
  178.     return sprintf("%*-=s", width || TEXT_WIDTH, text);
  179. }
  180.  
  181. varargs mixed get_attribute(mixed m, object ob) {
  182.     mapping attributes;
  183.  
  184.     if (!stringp(m))
  185.     return
  186.       transpose_array( ({
  187.         m_indices(
  188.           attributes =
  189.         get_extra_wizinfo(0)
  190.           [ATTRIBUTE_INDEX][m || previous_object()] ||
  191.         allocate_mapping(0,1)),
  192.         m_values(attributes)
  193.       }) ) ;
  194.     if ( ( attributes =
  195.          get_extra_wizinfo(0)[ATTRIBUTE_INDEX][ob || previous_object()]
  196.      ) && (mapping_contains(&m, attributes, m) ||
  197.            mapping_contains(&m, attributes, quote(m)))
  198.     )
  199.     return m;
  200.     return -1;
  201. }
  202.  
  203. varargs string *grab_file(string path, int start, int len) {
  204.     set_this_object(previous_object());
  205.     return explode(read_file(path, start, len), "\n");
  206.     /* Don't call this locally, unless you add code to set this_object() back */
  207. }
  208.  
  209. int index(mixed a, mixed d, int start) {
  210.     return member(start ? a[start..] : a , d);
  211. }
  212.  
  213. string pad(mixed str, int len, int padchar) {
  214.     string sp_format;
  215.  
  216.     str = to_string(str);
  217.     if (len < 0) {
  218.     if (padchar) {
  219.         sp_format = "%' '*.*s";
  220.         sp_format[2] = padchar;
  221.         return sprintf(sp_format, -len, -len, str);
  222.     }
  223.     return sprintf("%*.*s", -len, -len, str);
  224.     }
  225.     if (padchar) {
  226.     sp_format = "%' '-*.*s%s";
  227.     sp_format[2] = padchar;
  228.     return sprintf(sp_format, len, len, str, "");
  229.     }
  230.     return sprintf("%-*.*s%s", len, len, str, "");
  231. }
  232.  
  233. varargs void print(string text, int width) {
  234.     if (width > TEXT_WIDTH)
  235.     raise_error("Invalid size for argument 2 to print()\n");
  236.     if (width < 0)
  237.     raise_error("Bad width given to print(); must be >= 0.\n");
  238.     write(sprintf("%*-=s", width || TEXT_WIDTH, text));
  239. }
  240.  
  241. varargs mixed query_attribute(string m, object ob) {
  242.     mapping attributes;
  243.  
  244.     return
  245.       ( attributes =
  246.         get_extra_wizinfo(0)[ATTRIBUTE_INDEX][ob || previous_object()]
  247.       ) && (member(attributes, m) || member(attributes, quote(m)));
  248. }
  249.  
  250. static int remote_command_flag;
  251. int command(string str, object ob) {
  252.     /* Don't call this locally, unless you add code to set this_object() back */
  253.     int result;
  254.     object me;
  255.  
  256.     if (!ob) {
  257.     set_this_object(previous_object());
  258.     return efun::command(str);
  259.     }
  260.     if (ob == previous_object()) {
  261.     set_this_object(previous_object());
  262.     return efun::command(str);
  263.     }
  264.     remote_command_flag++;
  265.     me = this_object();
  266.     set_this_object(previous_object());
  267.     result = efun::command(str, ob);
  268.     set_this_object(me);
  269.     remote_command_flag = 0; /* global variable... */
  270.     return result;
  271. }
  272. int remote_command() { return remote_command_flag; }
  273.  
  274. int root(object ob) {
  275.     return member(file_name(ob || previous_object()), '#') == -1;
  276. }
  277.  
  278. int searcha(mixed a, mixed d, int start) {
  279.     return member(start ? a[start..] : a , d);
  280. }
  281.  
  282. static private string *strsub_formats = ({0, "\\<%s\\B","\\B%s\\>","\\<%s\\>"});
  283. string strsub(string input,string *patterns, int mode) {
  284.     int i;
  285.     string format;
  286.  
  287.     if ( (i = sizeof(patterns)) & 1)
  288.     raise_error("Odd length array given to strsub.\n");
  289.     if (!mode) {
  290.     while (i) {
  291.         input = implode(explode(input, patterns[<i--]), patterns[<i--]);
  292.     }
  293.     return input;
  294.     }
  295.     format = strsub_formats[mode];
  296.     while (i) {
  297.     input =
  298.       implode(
  299.         regexplode(input, sprintf(format, patterns[<i--])),
  300.         patterns[<i--]
  301.       );
  302.     }
  303.     return input;
  304. }
  305.  
  306. string this_verb() { return query_verb(); }
  307.  
  308.  
  309. int atoi(string str) { return to_int(str); }
  310.  
  311. int cp(string from, string to) {
  312.     string str;
  313.  
  314.     set_this_object(previous_object());
  315.     rm(to);
  316.     str = read_file(from);
  317.     write_file(to, str);
  318.     /* Don't call this locally, unless you add code to set this_object() back */
  319. }
  320.  
  321. int in_editor(object ob) { return query_editing(ob) && 1; }
  322.  
  323. mixed *keys(mapping m) { return m_indices(m); }
  324.  
  325. int mapp(mixed m) { return mappingp(m); }
  326.  
  327. int memused() {
  328.     return rusage()[2];
  329. }
  330.  
  331. object remove_shadow(object ob) {
  332.     if (!(ob))
  333.     if ( !(ob = previous_object()) )
  334.         return 0;
  335.     /* we are missing a test here to test for actual shadowing. */
  336.     funcall(bind_lambda(#'unshadow, ob || previous_object()));
  337.     return ob;
  338. }
  339.  
  340. mixed *reallocate(mixed *a, int size) {
  341.     if (size > sizeof(a))
  342.     return a + allocate(size - sizeof(a));
  343.     return a[0..size-1];
  344. }
  345.  
  346. string typeof(mixed m) {
  347.     switch(efun::typeof(m)) {
  348.       case T_OBJECT:
  349.     return "object";
  350.       case T_NUMBER:
  351.     return "int";
  352.       case T_POINTER:
  353.     return "vector";
  354.       case T_STRING:
  355.     return "string";
  356.       case T_MAPPING:
  357.     return "mapping";
  358.       default:
  359.     return "";
  360.     }
  361. }
  362.  
  363. static int last_destruct;
  364. static private mapping destructing = ([]);
  365. varargs void destruct(object ob, int flag) {
  366.     if (flag && !member(destructing, ob)) {
  367.     if (last_destruct != time())
  368.         destructing = allocate_mapping(0,0);
  369.     destructing += ([ob]);
  370.     ob->destructor();
  371.     }
  372.     efun::destruct(ob);
  373. }
  374.  
  375. /*
  376.  * The ls() function is no longer needed, as get_dir() can do the same
  377.  * work.
  378.  */
  379. void ls(string path) {
  380.     int max, i, len, tmp;
  381.     status trunc_flag;
  382.     mixed *dir;
  383.     set_this_object(previous_object());
  384.     if (!path)
  385.     path = ".";
  386.     dir = get_dir (path,3);
  387. #if defined(COMPAT_FLAG) && 0
  388.     if (path[0] == '/')
  389.     path = extract(path, 1);
  390.     if (path != "")
  391.     path += "/";
  392. #else
  393.     if (path != "/")
  394.     path += "/";
  395. #endif
  396.     if (!dir) {
  397.     write("No such directory.\n");
  398.     return;
  399.     }
  400.     if (sizeof(dir) > 999)
  401.     {
  402.     dir = dir[0..998];
  403.     trunc_flag = 1;
  404.     }
  405.     for(i = sizeof(dir); i--; ) {
  406.     if(dir[i--] == -2)
  407.         dir[i]+="/";
  408.     len = strlen(dir[i]);
  409.     if (len > max)
  410.         max = len;
  411.     }
  412.     ++max;
  413.     if (max > 79)
  414.     max = 79;
  415.     for (i=0; i < sizeof(dir); i+=2) {
  416.     string name;
  417.         name = dir[i];
  418.     tmp = strlen(name);
  419.     if (len + tmp > 79) {
  420.         len = 0;
  421.         write("\n");
  422.     }
  423.     write(name);
  424.     if (len + max > 79) {
  425.         write("\n");
  426.         len = 0;
  427.     } else {
  428.         write(extract(
  429. "                                                                                ",
  430.         80-max+tmp));
  431.         len += max;
  432.     }
  433.     }
  434.     write("\n");
  435.     if (trunc_flag) write("***TRUNCATED***\n");
  436. }
  437.  
  438. #if 0 /* This is probably too costly; just replace this_player(1) with
  439.          this_interactive() */
  440. /*
  441.  * Not accurate.
  442.  * Original behaviour: if arg _supplied_ return current_interactive.
  443.  */
  444.  
  445. varargs object this_player(int x)
  446. {
  447.     return x ? this_interactive() : efun::this_player();
  448. }
  449. #endif
  450.